#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 计算当前月份的日期范围
问题: 你的代码需要在当前月份中循环每一天，想找到一个计算这个日期范围的高效方法。
提示 : 
"""

from datetime import datetime, date, timedelta
import calendar

def recipe1():
    a_day = timedelta(days=1)
    first_day, last_day = get_month_range()
    while first_day < last_day:
        print(f"{first_day = }")
        first_day += a_day

def recipe2():
    for d in date_range(datetime(2012, 9, 1), datetime(2012,10,1),
                        timedelta(hours=6)):
        print(f"{d = }")

def date_range(start, stop, step):
    """生成器"""
    while start < stop:
        yield start
        start += step

def get_month_range(start_date=None):
    """返回一个由当前月份开始日和下个月开始日组成的元组对象"""
    if start_date is None:
        start_date = date.today().replace(day=1)
    _, day_in_month = calendar.monthrange(start_date.year, start_date.month)        # 找出该月的总天数, 返回包含星期和该月天数的元组
    end_date = start_date + timedelta(days=day_in_month)
    return (start_date, end_date)

def main():
    print('recipe1'.center(20, '*'))
    recipe1() 
    print('recipe2'.center(20, '*'))
    recipe2() 
if __name__ == '__main__':
    main()  